﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class ChucVuDAO
    {
        public static List<ChucVu> DSChucVuDel()
        {
            List<ChucVu> lst = new List<ChucVu>();
             try 
            {
                ChucVu cv;
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("sp_ChucVuDel_All", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    cv = new ChucVu();
                    cv.MaChucVu = Convert.ToInt32(dataReader["MaChucVu"]);
                    cv.TenChucVu = Convert.ToString(dataReader["TenChucVu"]);
                    cv.MoTa = Convert.ToString(dataReader["MoTa"]);
                    cv.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                    cv.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                    lst.Add(cv);

                }
                    cn.Close();
             } 
            catch (Exception) { }
            return lst;
                
        }

        public static List<ChucVu> DSChucVu(int ma)
        {
             List<ChucVu> lst = new List<ChucVu>();
            try 
            {       
                ChucVu cv;
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("Proc_ChucVu_GetAll", cn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@type", ma));
                SqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    cv = new ChucVu();
                    cv.MaChucVu = Convert.ToInt32(dataReader["MaChucVu"]);
                    cv.TenChucVu = Convert.ToString(dataReader["TenChucVu"]);
                    cv.MoTa = Convert.ToString(dataReader["MoTa"]);
                    cv.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                    cv.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                    lst.Add(cv);
                     }
                    cn.Close();
             } 
            catch (Exception) { }
            return lst;

        }

        public static ChucVu GetById(int ma)
        {
            
            ChucVu cv = null;
             try 
            {
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("Proc_ChucVu_GetById", cn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@maChucVu", ma));
                SqlDataReader dataReader = cmd.ExecuteReader();
                if(dataReader.Read())
                {
                    cv = new ChucVu();
                    cv.MaChucVu = ma;
                    cv.TenChucVu = Convert.ToString(dataReader["TenChucVu"]);
                    cv.MoTa = Convert.ToString(dataReader["MoTa"]);
                    cv.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                    cv.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                }
                cn.Close();
          } 
            catch (Exception) { }
            return cv;
        }

        public static List<int> GetDelFlg()
        {
             List<int> lst = new List<int>();
             try 
            {
                int temp;
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("Proc_ChucVu_GetDelFlg", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    temp = Convert.ToInt32(dataReader["DelFlg"]);
                    lst.Add(temp);
                }
                cn.Close();
             } 
            catch (Exception) { }
            return lst;

        }

        public static bool UpdateChucVu(ChucVu cv, int type)
        {
             try 
            {
             
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("sp_ChucVu_DelID", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@MaChucVu",cv.MaChucVu));

                cmd.Parameters.Add(new SqlParameter("@Type",type));
                cmd.ExecuteNonQuery();
                cn.Close();
             }
            catch (Exception) { }
            return true;
        }

        public static bool InsertChucVu(ChucVu cv)
        {
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("sp_ChucVu_Insert", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@TenChucVu", cv.TenChucVu));
            cmd.Parameters.Add(new SqlParameter("@MoTa", cv.MoTa));
            cmd.ExecuteNonQuery();
            cn.Close();
            return true;
        }
    }
}
